﻿using Azure.Core;
using FoodModel;
using J.FoodRepository.InterFace;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J.FoodRepository.Impl
{
    public class DishesReposittory : BackRepository<DishesModel>, IDishesReposittory
    {
        public DishesReposittory(ISqlSugarClient db) : base(db)
        {
        }
        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> DishesAdd(DishesModel model)
        {
            return await base.AddAsync(model);
        }

        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> DishesUpDate(DishesModel model)
        {
            return await base.UpdateAsync(model);
        }
        /// <summary>
        /// 菜单反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<DishesModel> DishesBack(int id)
        {
            var list = await base.GetModelAsync(id);
            return list;
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DishesDelete(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            foreach (var item in list)
            {
                base.DeleteAsync(item.DishId);
            }
            return list.Count;
        }
        /// <summary>
        /// 菜单开启点餐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DishesOpen(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            foreach (var item in list)
            {
                if (item.Selling == 1)
                {
                    item.Selling = 0;
                }
                else
                {
                    item.Selling = 1;
                }
                await base.UpdateAsync(item);

            }
            return list.Count;
        }
        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<DishesModel>> DishesShow(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            return list;
        }
        /// <summary>
        /// 菜单修改字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DishesOpenUpDate(string id, int GroupingId)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            foreach (var item in list)
            {
                item.GroupingId = GroupingId;
                await base.UpdateAsync(item);
            }
            return list.Count;
        }
        /// <summary>
        /// 菜单开启点餐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DishesOpens(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            foreach (var item in list)
            {
                if (item.Selling == 1)
                {
                    item.Selling = 0;
                }
                else
                {
                    return 0;
                }
                await base.UpdateAsync(item);

            }
            return list.Count;
        }
        /// <summary>
        /// 菜单关闭点餐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DishesShut(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.DishId.ToString()));
            foreach (var item in list)
            {
                if (item.Selling == 0)
                {
                    item.Selling = 1;
                }
                else
                {
                    return 0;
                }
                await base.UpdateAsync(item);

            }
            return list.Count;
        }
    }
}
